package com.zztl.infiniteBand.ui.activity

import android.app.Activity
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.Color
import android.view.View
import butterknife.OnClick
import com.githang.statusbar.StatusBarCompat
import com.uuzuche.lib_zxing.activity.CaptureFragment
import com.uuzuche.lib_zxing.activity.CodeUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BaseMvpActivity
import com.zztl.infiniteBand.presenter.ScanPresenter
import com.zztl.infiniteBand.storage.Constans
import com.zztl.infiniteBand.ui.view.mvpview.ScanView
import com.zztl.infiniteBand.util.PermissionUtils
import com.zztl.infiniteBand.util.UiUtils
import com.zztl.infiniteBand.util.toast
import kotlinx.android.synthetic.main.activity_scan.*


class ScanActivity : BaseMvpActivity<ScanView,ScanPresenter>(), ScanView {

 /*   @BindView(R.id.iv_select)
   lateinit  var iv_select: ImageView
*/

    private var captureFragment: CaptureFragment? = null

    private var scanResult: String? = null
    lateinit var mPresenter: ScanPresenter

    private var isLight: Boolean = false  //是否打开闪光灯

     private var analyzeCallback: CodeUtils.AnalyzeCallback = object : CodeUtils.AnalyzeCallback {
        override fun onAnalyzeSuccess(mBitmap: Bitmap, result: String) {

            scanResult = result
            mPresenter.checkResult(result, this@ScanActivity)
        }


        override fun onAnalyzeFailed() {
//            ToastUtils.show(this@ScanActivity, getString(R.string.无效的地址))
           toast(getString(R.string.无效的地址))
            finish()
        }
    }






    private val mPermissionGrant = object : PermissionUtils.PermissionGrant {
        override fun onPermissionGranted(requestCode: Int) {
            when (requestCode) {
                PermissionUtils.CODE_MULTI_PERMISSION -> selectFromAlum()
            }
        }
    }


    override fun setLayoutId(): Int = R.layout.activity_scan


    override fun initBase() {
        StatusBarCompat.setStatusBarColor(this, Color.BLACK)

        captureFragment = CaptureFragment()
        CodeUtils.setFragmentArgs(captureFragment, R.layout.layout_addr_camera)
        iv_select.visibility = View.VISIBLE
        // 为二维码扫描界面设置定制化界面
        captureFragment!!.analyzeCallback = analyzeCallback
        supportFragmentManager.beginTransaction().replace(R.id.fl_my_container, captureFragment!!).commit()

    }

    override fun createPresenter(): ScanPresenter {
        mPresenter = ScanPresenter()
        return mPresenter
    }

    override fun checkSuccess(isSuccess: Boolean) {
        if (isSuccess) {
            val intent = Intent()
            intent.putExtra(Constans.SCAN_RESULT, scanResult)
            setResult(Activity.RESULT_OK, intent)
            finish()
        } else {
//            ToastUtils.show(this@ScanActivity, getString(R.string.无效的地址))
         toast(getString(R.string.无效的地址))

        }
    }

    @OnClick(R.id.btn_back, R.id.iv_select, R.id.fl_my_container)
    fun onClick(view: View) {
        when (view.id) {
            R.id.btn_back -> finish()
            R.id.iv_select -> PermissionUtils.requestMultiPermissions(this, mPermissionGrant, PermissionUtils.CODE_LOAD_FILE)
            R.id.fl_my_container -> if (isLight) {
                isLight = false
                CodeUtils.isLightEnable(isLight)
            } else {
                isLight = true
                CodeUtils.isLightEnable(isLight)
            }
            else -> {
            }
        }

    }

    override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<String>,
                                            grantResults: IntArray) {
        PermissionUtils.requestPermissionsResult(this, requestCode, permissions, grantResults, mPermissionGrant)

    }

    private fun selectFromAlum() {
        val innerIntent = Intent(Intent.ACTION_GET_CONTENT)
        innerIntent.type = "image/*"
        startActivityForResult(innerIntent, REQUEST_CODE_SCAN_GALLERY)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        if (resultCode == Activity.RESULT_OK) {
            when (requestCode) {
                REQUEST_CODE_SCAN_GALLERY -> handleAlbumPic(data)
                else -> {
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data)
    }

    /**
     * 处理从相册选择的照片
     * @param data
     */

    private fun handleAlbumPic(data: Intent?) {
        //获取选中图片的路径
        runOnUiThread {
            val bitmap = UiUtils.getBitmapFormUri(this@ScanActivity, data!!.data)
            val result = mPresenter.scanningImage(bitmap!!)
            if (result != null) {
                scanResult = result.text
                mPresenter.checkResult(result.text, this@ScanActivity)
            } else {
//                ToastUtils.show(this@ScanActivity, "识别失败")
              toast( "识别失败")
            }
        }
    }

    companion object {
        private val REQUEST_CODE_SCAN_GALLERY = 10
    }
}
